/*
 *    Copyright (c) 2018-2025, shenghua All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: shenghua
 */

package com.chen.common.scheduler.aop;

import com.chen.common.scheduler.annotation.Job;
import com.chen.common.scheduler.config.JobUtil;
import com.chen.common.scheduler.entity.JobTask;
import com.chen.common.scheduler.inter.JobTaskConfigInter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 陈锦龙
 * @since 2022/3/16 16:12
 */
@Aspect
@Component
public class JobAop {

	@Resource
	private JobTaskConfigInter jobTaskConfigInter;

	@After("@annotation(job)")
	public void jobAfter(Job job){
		if (job.isQueryParam()){
			JobUtil.del();
		}
	}

	@Around("@annotation(job)")
	public Object jobAround(ProceedingJoinPoint point, Job job) throws Throwable {
		if (job.isQueryParam()){
			JobTask jobTask = jobTaskConfigInter.getTcByCode(job.value());
			JobUtil.set(jobTask != null ? jobTask.getParams() : "");
		}
		return point.proceed();
	}
}
